/*
 * Copyright 2006-2007 S W McIlroy
 * Licensed  under the  Apache License,  Version 2.0  (the "License");
 * you may not use  this file  except in  compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed  under the  License is distributed on an "AS IS" BASIS,
 * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
 * implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.mockfu;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;


public abstract class Matcher<T>
{
    private final String name;
    private final List<Matcher<Object>> nested;
    private volatile boolean descriptionImplied = false;

    public Matcher( String name, Object... arguments )
    {
        this.name = name;
        this.nested = Constraints.getMatchers( arguments );
    }

    public String getName()
    {
        return name;
    }

    public List<Matcher<Object>> getNested()
    {
        if (nested == null)
            return Collections.emptyList();
        return nested;
    }

    @Override
    public String toString()
    {
        StringBuilder s = new StringBuilder();
        s.append( getName() );
        s.append( "(" );
        for( Iterator<Matcher<Object>> i = getNested().iterator() ; i.hasNext() ; )
        {
            s.append( i.next().toString() );
            if( i.hasNext() )
                s.append( ", " );
        }
        s.append( ")" );
        return s.toString();
    }

    public ResultFormatter getResultFormatter()
    {
        return null;
    }

    public void setDescriptionImplied( boolean value )
    {
        this.descriptionImplied = value;
    }

    public boolean isDescriptionImplied()
    {
        return descriptionImplied;
    }

    public abstract boolean matches( T value );

    public abstract String description();

    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((nested == null) ? 0 : nested.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final Matcher<?> other = (Matcher<?>)obj;
        if (name == null)
        {
            if (other.name != null)
                return false;
        }
        else if (!name.equals(other.name))
            return false;
        if (nested == null)
        {
            if (other.nested != null)
                return false;
        }
        else if (!nested.equals(other.nested))
            return false;
        return true;
    }


}
